package com.tanhua.server.controller;

import com.tanhua.model.vo.NearUserVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.service.TanhuaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tanhua")
public class TanhuaController {

    @Autowired
    private TanhuaService tanhuaService;

    /**
     * 今日佳人：和当前用户相似度最高的一个推荐用户数据
     *    GET    /todayBest
     *    返回值：
     *        todayBest
     */
    @GetMapping("/todayBest")
    public ResponseEntity todayBest() {
        //1、调用service
        TodayBest vo = tanhuaService.todayBest();
        //2、构造返回
        return ResponseEntity.ok(vo);
    }

    /**
     * 首页推荐朋友
     *      GET  /recommendation
     *      Query请求参数
     *         page，pagesize
     *      返回值
     *          PageResult -- List<TodayBest>
     */
    @GetMapping("/recommendation")
    public ResponseEntity recommendation(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        //1、调用service查询
        PageResult vo = tanhuaService.recommendation(page,pagesize);
        //2、返回
        return ResponseEntity.ok(vo);
    }

    /**
     * 查看佳人信息
     *   GET      /{id}/personalInfo
     *   返回值：
     *      TodayBest
     */
    @GetMapping("/{id}/personalInfo")
    public ResponseEntity personalInfo(@PathVariable("id") Long userId) {
        TodayBest vo = tanhuaService.personalInfo(userId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 查看陌生人问题
     *   GET    /strangerQuestions
     *   query参数：
     *      userId
     *   返回值：
     *      String：问题内容
     */
    @GetMapping("/strangerQuestions")
    public ResponseEntity strangerQuestions(Long userId) {
        String content = tanhuaService.strangerQuestions(userId);
        return ResponseEntity.ok(content);
    }

    /**
     * 回复陌生人问题
     *   POST    /strangerQuestions
     *   Body参数：
     *      userId（对方的id）：接受消息的一方
     *      reply ： 回复内容
     */
    @PostMapping("/strangerQuestions")
    public ResponseEntity strangerQuestions(@RequestBody Map map) {
        String string = map.get("userId").toString();
        Long userId = Long.valueOf(string);
        String reply = map.get("reply").toString();
        //调用service
        tanhuaService.reply(userId,reply);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询附近的人
     *   get  /search
     *   参数：
     *      query
     *        gender，distance
     *   返回值：
     *      List<vo>
     */
    @GetMapping("/search")
    public ResponseEntity search(String gender,String distance) {
        List<NearUserVo> vos = tanhuaService.search(gender,distance);
        return ResponseEntity.ok(vos);
    }
}
